package com.h2cloud.JUCTest.TwoParseTermination;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Test11 {
	public static void main(String[] args) throws InterruptedException {
		TwoParseTermination twoParseTermination = new TwoParseTermination();
		twoParseTermination.start();
		Thread.sleep(3000);  // 让监控线程执行一会儿
		twoParseTermination.stop(); // 停止监控线程

	}
}


@Slf4j
class TwoParseTermination {
	Thread monitorThread;

	public void start() {

		monitorThread = new Thread(() -> {
			while (true) {
				if (Thread.currentThread().isInterrupted()) {
					log.debug("线程结束...正在料理后事中...");
					break;
				}

				try {
					Thread.sleep(500); // 情况一
					log.debug("执行监控的功能"); // 情况二

				} catch (InterruptedException e) {
					// 因为 sleep时被打断会清除打断标记，所以重新设置打断标记
					Thread.currentThread().interrupt();
					e.printStackTrace();
				}
			}
		});
		monitorThread.start();
	}

	public void stop() {
		monitorThread.interrupt();
	}
}
